﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.UC.订单一览新.订单预览新.service;
using DXApplicationTangche.UC.订单一览新.销售确认单;
using DXApplicationTangche.UC.下单MTO.Service;
using static DXApplicationTangche.UC.门店下单.form.Frm订单一览;
using DXApplicationTangche.UC.销售记录;
using DevExpress.XtraGrid.Demos.util;

namespace DXApplicationTangche.UC.门店下单.下单
{
    public partial class Form补充定制下单 : DevExpress.XtraEditors.XtraForm
    {
        private List<String> quoteIds = new List<string>();
        private List<SaleConfirmQueryTitileDTO> saleConfirmQueryTitileDTO定制 { get; set; } = new List<SaleConfirmQueryTitileDTO>();
        private CustomerPDTO customerPDTO = new CustomerPDTO();
        /// <summary>
        /// 订单充血类
        /// </summary>
        private OrderModel orderModel = new OrderModel();

        public Form补充定制下单(String dateTimeStr, String mobile, CustomerPDTO customerPDTO)
        {
            InitializeComponent();
            this.customerPDTO = customerPDTO;
            this.saleConfirmQueryTitileDTO定制 =
                销售确认Service.getSaleConfirmListByBusinessTypeAndSynchronizeFalseAndMobileAndOrderDateBetween(dateTimeStr, mobile);
            页面刷新();
        }

        private void 页面刷新()
        {
            this.gridControl待补充.DataSource = this.saleConfirmQueryTitileDTO定制;
            this.gridView待补充.RefreshData();
            this.gridControl待下单.DataSource = this.orderModel.orderCommitDTO返回s;
            this.gridView待下单.RefreshData();
        }

        private void 补充定制下单信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            String id = FunctionHelper.GetgridViewValue(this.gridView待补充, this.gridView待补充.FocusedRowHandle, "id");
            String refBarcode = FunctionHelper.GetgridViewValue(this.gridView待补充, this.gridView待补充.FocusedRowHandle, "refBarcode");
            String styleSizeCd = FunctionHelper.GetgridViewValue(this.gridView待补充, this.gridView待补充.FocusedRowHandle, "styleSizeCd");
            String materialCode = FunctionHelper.GetgridViewValue(this.gridView待补充, this.gridView待补充.FocusedRowHandle, "materialCode");
            String ordreNumber = FunctionHelper.GetgridViewValue(this.gridView待补充, this.gridView待补充.FocusedRowHandle, "quantity");
            if (String.IsNullOrEmpty(id) || String.IsNullOrEmpty(refBarcode) || String.IsNullOrEmpty(styleSizeCd))
            {
                return;
            }
            Form补充定制订信息 form = new Form补充定制订信息(refBarcode, styleSizeCd, id, materialCode,ordreNumber);
            form.ShowDialog();
            if (form.是否确认)
            {
                this.buildOrderModelOrderCommitDTO(form.orderCommitDTO, form.备注DTOs);
                this.saleConfirmQueryTitileDTO定制.Remove(this.saleConfirmQueryTitileDTO定制[this.gridView待补充.FocusedRowHandle]);
                this.quoteIds.Add(id);
            }
            页面刷新();
        }

        public void buildOrderModelOrderCommitDTO(OrderCommitDTO返回 orderCommitDTO, List<备注DTOs> 备注DTOs)
        {
            this.orderModel.buildAddOrderCommitDto(orderCommitDTO, 备注DTOs);
        }

        private void Form补充定制下单_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (this.orderModel.orderCommitDTO返回s.Count > 0)
            {
                foreach (OrderCommitDTO返回 orderCommitDto in this.orderModel.orderCommitDTO返回s)
                {
                    //给订单客户属性赋值
                    orderCommitDto.build给客户赋值新(this.customerPDTO);
                }
                //下单确认后调用完成接口
                new Form销售记录(this.orderModel, this.customerPDTO.customerName, Enum订单类别.销售订单, this.quoteIds).ShowDialog();
            }
        }
    }
}